
package com.work.db.entity;

import java.io.Serializable;
import java.util.*;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.*;

/*@NamedNativeQuery(name="getWorkMonths", 
		query="SELECT DICTINCT MONTH(date) FROM workovertime.workday wd WHERE YEAR(date) = :year",
		resultClass=Integer.class)*/

@Entity
public class WorkDay extends BaseEntity implements Serializable {

	@Transient
	private static final long serialVersionUID = -383968119155157119L;

	@NotNull
	@Column(name = "DATE")
	private Date date;

	@Column(name = "COMMENT")
	private String comment;

	@Column(name = "PRESENTS_TYPE")
	@Enumerated(EnumType.STRING)
	private PresentsTypeEnum presentsType;

	@OneToOne(optional = true, cascade = CascadeType.ALL)
	private Presents presents;

	@ManyToOne(optional = true, cascade = CascadeType.ALL)
	private LeaveOfAbsence leaveOfAbsence;

	@OneToMany(mappedBy = "workDay", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private List<Overtime> overtimes;

	@ManyToOne(optional = true, cascade = CascadeType.ALL)
	private Employer employer;

	public WorkDay() {
		super();
	}

	public Date getDate()
	{
		return date;
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public Presents getPresents()
	{
		return presents;
	}

	public void setPresents(Presents presents)
	{
		this.presents = presents;
	}

	public LeaveOfAbsence getLeaveOfAbsence()
	{
		return leaveOfAbsence;
	}

	public void setLeaveOfAbsence(LeaveOfAbsence leaveOfAbsence)
	{
		this.leaveOfAbsence = leaveOfAbsence;
	}

	public List<Overtime> getOvertimes()
	{
		return overtimes;
	}

	public void setOvertimes(List<Overtime> overtimes)
	{
		this.overtimes = overtimes;
	}

	public String getComment()
	{
		return comment;
	}

	public void setComment(String comment)
	{
		this.comment = comment;
	}

	public PresentsTypeEnum getPresentsType()
	{
		return presentsType;
	}

	public void setPresentsType(PresentsTypeEnum presentsType)
	{
		this.presentsType = presentsType;
	}

	public Employer getEmployer()
	{
		return employer;
	}

	public void setEmployer(Employer employer)
	{
		this.employer = employer;
	}

	public int getWorkTimeMinutes()
	{
		int totalOvertime = getOverTimeMinutes();
		int presentsTime = getPresentsTimeMinutes();
		return totalOvertime + presentsTime;
	}

	public int getPresentsTimeMinutes()
	{
		if (presents != null) {
			return presents.getMinutes();
		}

		return 0;
	}

	public int getOverTimeMinutes()
	{
		int minutes = 0;
		if (overtimes != null) {
			for (Overtime overtime : overtimes) {
				minutes += overtime.getMinutes();
			}
		}

		return minutes;
	}

	@Override
	public String toString()
	{
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("date", getDate()).append("comment", getComment()).append("presentsType", getPresentsType()).append("presents", getPresents()).append("leaveOfAbsence", getLeaveOfAbsence()).toString();
	}
}
